from __future__ import (absolute_import, division, print_function, unicode_literals)

import datetime

import backtrader as bt
import matplotlib.pyplot as plt
plt.rcParams["figure.dpi"] = 200
class VolumeIndicator(bt.Indicator):
    lines = ('volume',)

    def __init__(self):
        self.lines.volume = self.data.volume

class MyStrategy(bt.Strategy):
    def __init__(self):
        # https://www.backtrader.com/docu/talibindautoref/#ta-lib-indicator-reference
        self.ad_line= bt.talib.AD(self.data.high, self.data.low, self.data.close, self.data.volume)
        # self.ad_line.plotinfo.plotabove=True
        self.obv=bt.talib.OBV(self.data.close, self.data.volume)
        self.sma=bt.talib.SMA(timeperiod=5)
        # self.ad_line.plotinfo.plotmaster = self.obv
        # self.obv.plotinfo.plotymargin = 0.2  # 10% 的边距
        # self.obv.plotinfo.subplot = False  # 在主图上显示
        

        
        

data = bt.feeds.YahooFinanceCSVData(
    dataname='csv/{}.csv'.format('COCH'),
    fromdate=datetime.datetime(2023, 10, 1),
    # todate=datetime.datetime(2017, 1, 1)
    todate=datetime.datetime.now()
)

cerebro = bt.Cerebro(stdstats=False)
cerebro.adddata(data)
cerebro.addstrategy(MyStrategy)
cerebro.run()
cerebro.plot(style='candle')